const app = getApp();
let downloadTask;
Page({
  data: {
    list:[],
    page: 0,
  },
  onLoad: function (options) {
    let that = this;
    that.getInfo();
  },
  getInfo(){
    let that = this;
    let page = that.data.page + 1;
    app.request('library/libraryList', { page }, 'GET', false, function (res) {
      let list = that.data.list;
      list.push(...res.data.data.data)
      that.setData({
        pageShow: true,
        list: list,
      }, () => {
        // that.getFileManager();
        // that.getDownload();
      })
    })
  },
  getDownload(){
    let that = this;
    let fileList = wx.getStorageSync('fileList') || [];
    let list = that.data.list;
    fileList.map((v,k)=>{
      for(let i=0;i<list.length;i++){
        if (v.file_src == list[i].file_path){
          list[i].download = 1;
          return;
        }
      }
    })
    that.setData({
      list, fileList
    })
  },
  // getFileManager(){
  //   let that = this;
  //   const fileInfo = wx.getFileSystemManager();
  //   fileInfo.getSavedFileList({
  //     success(res) {
  //       // 取到本地文件列表
  //       let fileList = res.fileList;
  //       let list = that.data.list;
  //       fileList.map((v,k)=>{
  //         for(let i=0;i<list.length-1;i++){
  //           if (v.filePath == list[i].file_path){
  //             list[i].download = 1;
  //             return;
  //           }
  //         }
  //       })
  //       that.setData({
  //         fileList, list
  //       })
  //     }
  //   })
  // },
  download(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let list = that.data.list;
    let library_id = list[index].id;
    let path = list[index].file_path;
    wx.showLoading({
      title: '下载中...',
      mask: true,
    })
    downloadTask = wx.downloadFile({
      url: path,
      success(res) {
        wx.saveFile({
          tempFilePath: res.tempFilePath,
          success(res) {
            wx.hideLoading();
            let fileItem={'file_src': path};
            let fileList = wx.getStorageSync('fileList') || [];
            fileList.push(fileItem);
            wx.setStorageSync('fileList', fileList)
            // that.getFileManager();
            that.getDownload();
          }
        })
      }
    })
  },
  openDocument(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let filePath = that.data.list[index].file_path;
    downloadTask = wx.downloadFile({
      url: filePath,
      success(res) {
        wx.openDocument({
          filePath: res.tempFilePath,
          success: function (res) {
            console.log('打开文档成功')
          },
          fail(res) {
            wx.showToast({
              title: '文档格式不支持',
              icon: 'none'
            })
          }
        })
      }
    })
    
  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {
    this.getInfo();
  },
  onShareAppMessage: function () {
    return{
      path: '/pages/index/index'
    }
  }
})